package com.jason.platform.supervision.log;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @ClassName: Log
 * @Description: 日志打印
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-23 11:10:52
 */
public class Log {

    protected static String FILE_PATH = "/sdcard0/SupervisionPlatform/log.txt";
    protected static int FILE_MAX_LENGTH = 10*1024*1024;
    private static boolean DEBUG = true;
    private static boolean IS_PRINT = true;
    private static boolean IS_SAVE_FILE = true;

    public Log() {
    }

    public static void i(String tag, String msg) {
        if(DEBUG) {
            if(IS_PRINT) {
                Log.i(tag, String.format("[DEBUG] %s", new Object[]{msg}));
            }

            if(IS_SAVE_FILE) {
                LogFile.writeLogAppend(tag, msg);
            }
        }

    }

    public static void i(String tag, String msg, Throwable thr) {
        if(DEBUG) {
            if(IS_PRINT) {
                Log.i(tag, String.format("[DEBUG] %s", new Object[]{msg}), thr);
            }

            if(IS_SAVE_FILE) {
                LogFile.writeLogAppend(tag, msg);
                LogFile.writeLogAppend(tag, getErrorInfo(thr));
            }
        }

    }

    public static void v(String tag, String msg) {
        if(DEBUG) {
            if(IS_PRINT) {
                Log.v(tag, String.format("[DEBUG] %s", new Object[]{msg}));
            }

            if(IS_SAVE_FILE) {
                LogFile.writeLogAppend(tag, msg);
            }
        }

    }

    public static void v(String tag, String msg, Throwable thr) {
        if(DEBUG) {
            if(IS_PRINT) {
                Log.v(tag, String.format("[DEBUG] %s", new Object[]{msg}), thr);
            }

            if(IS_SAVE_FILE) {
                LogFile.writeLogAppend(tag, msg);
                LogFile.writeLogAppend(tag, getErrorInfo(thr));
            }
        }

    }

    public static void e(String tag, String msg) {
        if(DEBUG) {
            if(IS_PRINT) {
                Log.e(tag, String.format("[DEBUG] %s", new Object[]{msg}));
            }

            if(IS_SAVE_FILE) {
                LogFile.writeLogAppend(tag, msg);
            }
        }

    }

    public static void e(String tag, String msg, Throwable thr) {
        if(DEBUG) {
            if(IS_PRINT) {
                Log.e(tag, String.format("[DEBUG] %s", new Object[]{msg}), thr);
            }

            if(IS_SAVE_FILE) {
                LogFile.writeLogAppend(tag, msg);
                LogFile.writeLogAppend(tag, getErrorInfo(thr));
            }
        }

    }

    public static void d(String tag, String msg) {
        if(DEBUG) {
            if(IS_PRINT) {
                Log.d(tag, String.format("[DEBUG] %s", new Object[]{msg}));
            }

            if(IS_SAVE_FILE) {
                LogFile.writeLogAppend(tag, msg);
            }
        }

    }

    public static void d(String tag, String msg, Throwable thr) {
        if(DEBUG) {
            if(IS_PRINT) {
                Log.d(tag, String.format("[DEBUG] %s", new Object[]{msg}), thr);
            }

            if(IS_SAVE_FILE) {
                LogFile.writeLogAppend(tag, msg);
                LogFile.writeLogAppend(tag, getErrorInfo(thr));
            }
        }

    }

    public static void w(String tag, String msg) {
        if(DEBUG) {
            if(IS_PRINT) {
                Log.w(tag, String.format("[DEBUG] %s", new Object[]{msg}));
            }

            if(IS_SAVE_FILE) {
                LogFile.writeLogAppend(tag, msg);
            }
        }

    }

    public static void w(String tag, String msg, Throwable thr) {
        if(DEBUG) {
            if(IS_PRINT) {
                Log.w(tag, String.format("[DEBUG] %s", new Object[]{msg}), thr);
            }

            if(IS_SAVE_FILE) {
                LogFile.writeLogAppend(tag, msg);
                LogFile.writeLogAppend(tag, getErrorInfo(thr));
            }
        }

    }

    public static void setDebug(boolean debug) {
        DEBUG = debug;
    }

    public static void setPrint(boolean print) {
        IS_PRINT = print;
    }

    public static void setPrintFile(boolean printFile) {
        IS_SAVE_FILE = printFile;
    }

    public static void setLogFilePath(String savePath) {
        FILE_PATH = savePath;
    }

    public static void setMaxLogFileLength(int max) {
        FILE_MAX_LENGTH = max;
    }

    private static String getErrorInfo(Throwable throwable) {
        StringWriter writer = new StringWriter();
        PrintWriter localPrintWriter = new PrintWriter(writer);
        throwable.printStackTrace(localPrintWriter);
        localPrintWriter.close();
        return writer.toString();
    }
}
